package com.tmt.im.manager.controller;

import com.alibaba.fastjson2.JSONObject;
import com.tmt.data.jpa.service.beans.InvalidAccessToken;
import com.tmt.data.jpa.service.beans.UserInfoVo;
import com.tmt.data.jpa.service.beans.UserLoginVo;
import com.tmt.data.jpa.service.beans.UserLogoutVo;
import com.tmt.im.manager.service.NotifyService;
import com.tmt.springboot.common.restful.IExecuteResult;
import com.tmt.springboot.common.restful.ResultWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.session.Session;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * 应用场景：
 * 当用户直接从授权服务器上登出时，授权服务器会向所有的客户系统发送用户登出消息，
 * 以便客户系统清除登出用户的Session数据。
 *
 * @Description TODO
 * @Author wuyi
 * @Date 2024/7/26 9:38
 * @Version 1.0
 **/
@Slf4j
@Async("asyncTaskExecutor")
@Tag(name = "授权服务器通知处理接口", description = "处理授权中心发来的用户相关的各类通知")
@RestController
@RequestMapping("${api-url-prefix}/externalNotify")
public class ExternalNotifyController<S extends Session> {

    private final NotifyService<S> notifyService;

    public ExternalNotifyController(NotifyService<S> notifyService) {
        this.notifyService = notifyService;
    }

    @Operation(summary = "用户登录通知处理接口", description = "用户登录通知处理过程中会清除ignite缓存中的用户登出信息")
    @PostMapping(value = "/login")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> loginNotify(@RequestBody String info) {
        UserLoginVo vo = JSONObject.parseObject(info, UserLoginVo.class);
        IExecuteResult<Boolean> result = notifyService.processLoginNotify(vo);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "用户登出通知处理接口", description = "用户登出通知处理接口")
    @PostMapping(value = "/logout")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> logoutNotify(@RequestBody String info) {
        UserLogoutVo vo = JSONObject.parseObject(info, UserLogoutVo.class);
        IExecuteResult<Boolean> result = notifyService.processLogoutNotify(vo);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "用户注册通知处理接口", description = "用户注册通知处理接口")
    @PostMapping(value = "/register")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> registerNotify(@RequestBody String info) {
        UserInfoVo vo = JSONObject.parseObject(info, UserInfoVo.class);
        IExecuteResult<Boolean> result = notifyService.processUserRegisterNotify(vo);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "用户信息变更通知处理接口", description = "用户信息变更通知处理接口")
    @PostMapping(value = "/userChanged")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> userChanged(@RequestBody String info) {
        UserInfoVo vo = JSONObject.parseObject(info, UserInfoVo.class);
        IExecuteResult<Boolean> result = notifyService.processUserChangedNotify(vo);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "令牌注销通知接口", description = "令牌注销通知接口")
    @PostMapping(value = "/unregisterToken")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> unregisterToken(@RequestBody String info) {
        InvalidAccessToken vo = JSONObject.parseObject(info, InvalidAccessToken.class);
        IExecuteResult<Boolean> result = notifyService.processUnregisterTokenNotify(vo);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

}
